$(document).ready(function() {
	

	var id = (GetQueryString("id"));

	jwplayer("container").setup({
		flashplayer : "./flash/player.swf",
		"playlist.position" : "right",
		"playlist.size" : 200,
		height : 270,
		width : 570,
		repeat : 'list',
		'plugins' : 'hd'
	});
	
	if(id == null){
		playall();
		
	}else{
		playone(id);
		
	}
});

function loadlist(videolist, i, playlist, qword) {
	var myurl = videolist[i].file;
	var myimage = videolist[i].image;
	var mydescription = videolist[i].description;
	var myduration = videolist[i].duration;
	$.ajaxQueue.offer({
		type : "GET",
		url : "./parselink",
		data : {
			url : myurl,
			image : myimage,
			description : mydescription,
			duration : myduration
		},
		
		success: function(data, textStatus, jqXHR) {
			var i = playlist.length;
			var newItem = eval('(' + data + ')');
			newItem.title = qword;
			playlist.push(newItem);
			jwplayer().load(playlist);
		
	    }
		
		
	});
}

function video(duration, file, image) {
	this.duration = duration;
	this.file = file;
	this.image = image;
}

function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return unescape(r[2]);
	return null;
}

function playall() {
	var videolist = new Array();
	var du = $(".linkduration span", window.opener.document);
	du.each(function() {
		var e = new video();
		e.duration = $(this).text().split(":")[1];
		videolist.push(e);

	});

	var des = $(".linktitle a", window.opener.document);
	var i = 0;
	des.each(function() {
		videolist[i].description = $(this).text();
		i++;
	});

	var img = $(".linkimg a img", window.opener.document);
	i = 0;
	img.each(function() {
		videolist[i].image = $(this).attr("src");
		i++;
	});

	var vurl = $(".linkimg span", window.opener.document);
	i = 0;
	vurl.each(function() {
		videolist[i].file = $(this).text();
		i++;
	});

	var oqword = $(".textinput", window.opener.document);
	var qword = oqword.attr("value");

	var playlist = [];

	for ( var i = 0; i < videolist.length; i++) {
		loadlist(videolist, i, playlist, qword);
	}

}

function playone(id) {

	var duration = $("#" + id + " .linkduration span", window.opener.document)
			.text().split(":")[1];
	var description = $("#" + id + " .linktitle a", window.opener.document)
			.text();
	var image = $("#" + id + " .linkimg a img", window.opener.document).attr(
			"src");
	var file = $("#" + id + " .linkimg span", window.opener.document).text();
	var oqword = $(".textinput", window.opener.document);
	var qword = oqword.attr("value");
	var playlist = [];
	var videolist = [];
	var e = new video();
	e.duration = duration;
	e.description = description;
	e.image = image;
	e.file = file;
	videolist.push(e);
	loadlist(videolist, 0, playlist, qword);
}
